﻿using Max.PH4_2102A.Shopping.DAL;
using Max.PH4_2102A.Shopping.Domain;
using Max.PH4_2102A.Shopping.IRespository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4_2102A.Shopping.Respository
{
    /// <summary>
    /// 分类的仓储实现
    /// </summary>
    public class CategoryRespository : ICategoryRespository
    {
        EFCoreHelper _db;
        /// <summary>
        /// 构造函数注入上下文
        /// </summary>
        /// <param name="db"></param>
        public CategoryRespository(EFCoreHelper db)
        {
            _db = db;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(CategoryModel model)
        {
            _db.Categories.Remove(model);
            int result = _db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public int Delete(List<CategoryModel> models)
        {
            //实现批量删除（Range - 删除范围）
            _db.Categories.RemoveRange(models);
            int result = _db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Insert(CategoryModel model)
        {
            _db.Categories.Add(model);
            int result = _db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<CategoryModel> Query()
        {
            var list = _db.Categories;
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(CategoryModel model)
        {
            _db.Categories.Update(model);
            int result = _db.SaveChanges();
            return result;
        }
    }
}
